package com.hngy.xpq.android_practice.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hngy.xpq.android_practice.R;
import com.hngy.xpq.android_practice.bean.Comm;
import com.hngy.xpq.android_practice.merchant_main.Comm.CommMerchantActivity;

import java.util.List;

public class CommAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private List<Comm> list;
    CommMerchantActivity cma;

    public CommAdapter(Context context, List<Comm> list) {
        this.inflater = LayoutInflater.from(context);
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 :list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null){
            convertView = inflater.inflate(R.layout.item_comm,null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Comm comm = (Comm) getItem(position);
        //将byte型的图片转换成drawable型
        byte[] c = comm.getImage();
        Bitmap b = BitmapFactory.decodeByteArray(c,0,c.length);
        BitmapDrawable bd = new BitmapDrawable(b);

        viewHolder.image.setImageDrawable(bd);
        viewHolder.text_name.setText(comm.getComm_name());
        viewHolder.text_price.setText(comm.getComm_price());
        viewHolder.text_types.setText(comm.getComm_types());
        viewHolder.text_status.setText(comm.getStatus());
        return convertView;

    }

    class ViewHolder{
        ImageView image;
        TextView text_name,text_types,text_price,text_status;
        public ViewHolder(View v){
            image = v.findViewById(R.id.item_image);
            text_name = v.findViewById(R.id.item_name);
            text_types = v.findViewById(R.id.item_types);
            text_price = v.findViewById(R.id.item_price);
            text_status = v.findViewById(R.id.item_status);
        }
    }

}
